package xyz.bernkastel.goods_in_great_demand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.bernkastel.goods_in_great_demand.common.CustomException;
import xyz.bernkastel.goods_in_great_demand.dto.SetmealDto;
import xyz.bernkastel.goods_in_great_demand.entity.Setmeal;
import xyz.bernkastel.goods_in_great_demand.entity.SetmealGoods;
import xyz.bernkastel.goods_in_great_demand.mapper.SetmealMapper;
import xyz.bernkastel.goods_in_great_demand.service.SetmealGoodsService;
import xyz.bernkastel.goods_in_great_demand.service.SetmealService;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealGoodsService setmealGoodsService;

    /**
     * 新增套餐，同时增加商品与套餐关系
     *
     * @param setmealDto
     */
    @Transactional
    public void saveWithGoods(SetmealDto setmealDto) {
        //保存套餐信息
        this.save(setmealDto);

        //保存套餐与商品关系
        List<SetmealGoods> setmealGoods = setmealDto.getSetmealGoods();
        setmealGoods.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());//为套餐关系表里面添加套餐id

        setmealGoodsService.saveBatch(setmealGoods);
    }

    /**
     * 移除套餐
     *
     * @param ids
     */
    @Transactional
    public void removeWithGoods(List<Long> ids) {
        //查询套餐状态，确定是否可以删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId, ids);
        queryWrapper.eq(Setmeal::getStatus, 1);
        int count = this.count(queryWrapper);
        if (count > 0) {
            throw new CustomException("套餐正在售卖中，不能删除！");

        }

        //如果可以删除，先删除套餐表中数据
        this.removeByIds(ids);


        //删除关系表中数据
        LambdaQueryWrapper<SetmealGoods> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(SetmealGoods::getSetmealId, ids);
        setmealGoodsService.remove(queryWrapper1);


    }

    /**
     * 修改套餐
     *
     * @param setmealDto
     */
    @Transactional
    public void updateWithGoods(SetmealDto setmealDto) {
        //先删
        //暂定不需要
//        //查询套餐状态，确定是否可以删除
//        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
//        queryWrapper.in(Setmeal::getId,ids);
//        queryWrapper.eq(Setmeal::getStatus,1);
//        int count=this.count(queryWrapper);
//        if(count>0)
//        {
//            throw new CustomException("套餐正在售卖中，不能删除！");
//
//        }

        //删除套餐表中数据
        this.removeById(setmealDto.getId());


        //删除关系表中数据
        LambdaQueryWrapper<SetmealGoods> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(SetmealGoods::getSetmealId, setmealDto.getId());
        setmealGoodsService.remove(queryWrapper1);


        //保存套餐信息
        this.save(setmealDto);

        //保存套餐与商品关系

        List<SetmealGoods> setmealGoods = setmealDto.getSetmealGoods();
        setmealGoods.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());//为套餐关系表里面添加套餐id

        setmealGoodsService.saveBatch(setmealGoods);

    }
}
